package com.atguigu.educms.controller;

import com.atguigu.commonutils.R;
import com.atguigu.educms.entity.CrmBanner;
import com.atguigu.educms.service.CrmBannerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.sql.Wrapper;
import java.util.*;

/**
 * @Author: GengKY
 * @Date: 2021/9/22 15:21
 */
@RestController
@RequestMapping("/educms/bannerfront")
//@CrossOrigin
public class BannerFrontController {

    @Autowired
    private CrmBannerService bannerService;

    /**
     * 查询所有banner
     *
     * @return
     */
    @GetMapping("getAllBanner")
    public R getAllBanner() {
//        List<CrmBanner> list = bannerService.selectAllBanner();
        QueryWrapper<CrmBanner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted", 0);
        List<CrmBanner> list1 = bannerService.list(queryWrapper);
        return R.ok().data("list", list1);
    }


    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 根据ID删除轮播图
     *
     * @param banid
     * @return
     */
    @DeleteMapping("deleteBanner/{banid}")
    public R deleteBanner(@PathVariable("banid") String banid) {
        boolean flag = bannerService.removeById(banid);
        if (flag) {
            redisTemplate.delete("banner::selectIndexList");
            return R.ok();
        } else {
            return R.error();
        }
    }


}
